<template>
  <div><!-- 顶部面包屑标识与导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台首页
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/admin/temp/category/list' }">其他管理</el-breadcrumb-item>
      <el-breadcrumb-item>发布通知</el-breadcrumb-item>
    </el-breadcrumb>

<el-divider content-position="left"></el-divider>

    <el-form :inline="true" :model="editForm1" >
      <el-form-item label="发布人">
        <el-input v-model="editForm1.name" prop="name"></el-input>
      </el-form-item>
      <el-form-item label="标题">
        <el-input v-model="editForm1.title" prop="title"></el-input>
      </el-form-item>
      <el-form-item label="发布时间">
        <el-input v-model="editForm1.gmtCreate" prop="gmtCreat"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('editForm1')">查询</el-button>
      </el-form-item>
    </el-form>
<!-- 数据表格 -->
<el-divider content-position="left">公告列表</el-divider>
<el-table :data="tableData" border style="width: 100%">
<el-table-column prop="id" label="公告ID" width="200" align="center"></el-table-column>

<el-table-column prop="name" label="发布人" width="200" align="center"
                 :show-overflow-tooltip="true"></el-table-column>
<el-table-column prop="title" label="标题" header-align="center"
                 :show-overflow-tooltip="true"></el-table-column>
<el-table-column prop="gmtCreate" label="发布时间" width="300" align="center"></el-table-column>
<el-table-column label="操作" width="100" align="center">
  <template slot-scope="scope">
    <el-button type="text" align="center" icon="el-icon-search"
               @click="openEditDialog(scope.row)"></el-button>
    <el-button type="text"  align="center"   icon="el-icon-delete"
               :disabled="scope.row.isParent == 1"
               @click="openDeleteConfirm(scope.row)"></el-button>
  </template>
</el-table-column>
</el-table>
    <!-- 操作区域 -->
    <div style="margin: 0 0 20px 0;">
      <el-button type="primary" size="small" @click="$router.push('/sys/notice/add-new')">发布通知</el-button>
    </div>
<!-- 返回上层 -->
<div v-if="currentParentId != 0" style="margin-top: 10px; text-align: right;">
<el-button size="small" @click="goBack()">返回</el-button>
</div>

<!-- 分页控件 -->
<div style="margin: 10px 0; text-align: right;">
<el-pagination
    @current-change="changePage"
    layout="total, prev, pager, next"
    :hide-on-single-page="true"
    :page-size="pageSize"
    :current-page="currentPage"
    :total="total">
</el-pagination>
</div>
<!-- 修改类别的弹出表单Dialog -->
<el-dialog title="通知详情" :visible.sync="dialogFormVisible">
<el-form :model="editForm" :rules="editRules" label-width="130px">
  <el-form-item label="标题" prop="title">
    <el-input v-model="editForm.title"></el-input>
  </el-form-item>
  <el-form-item label="发布人" prop="name">
    <el-input v-model="editForm.name"></el-input>
  </el-form-item>
  <el-form-item label="发布内容" prop="time">
    <el-input type="textarea" v-model="editForm.content"></el-input>
  </el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
  <el-button @click="dialogFormVisible = false">退出</el-button>
</div>
</el-dialog>
</div>

</template>

<script>
export default {
  data() {
    return {
      // 表格数据
      tableData: [],
      // 当前类别列表的父级类别ID
      currentParentId: 0,
      // 浏览历史，此数据主要用于返回功能
      history: [],
      // 编辑对话框的可见性
      dialogFormVisible: false,
      // 编辑表单
      editForm1: {
        name: '',
        title:'',
        gmtCreate:'',
      },
      editForm: {
        id: '',
        name: '',
        title:'',
        gmtCreate:'',
        content:'',
      },
      // 编辑表单的验证规则
      editRules: {
        name: [
          {required: true, message: '请输入类别名称', trigger: 'blur'},
          {min: 2, max: 15, message: '类别名称长度必须在 2 到 15 个字符', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入关键词列表', trigger: 'blur'},
          {min: 2, max: 50, message: '关键词列表长度必须在 2 到 50 个字符', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入排序序号', trigger: 'blur'},
          {pattern: /^([0-9]{1}|[1-9]{1}[0-9]{1})$/, message: '排序序号必须是 0~99 之间的数字', trigger: 'blur'}
        ]
      },
    }
  },
  methods: {
    submitForm() {
      console.log('submit!');
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }
      let url = 'http://localhost:9090/sys/notice/select';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.editForm1);
      console.log('formData = ' + formData);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data.list;
          this.currentPage = jsonResult.data.currentPage;
          this.pageSize = jsonResult.data.pageSize;
          this.total = jsonResult.data.total;
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    },
    // 返回
    goBack() {
      console.log('goBack, start...');
      for (let i = 0; i < this.history.length; i++) {
        console.log(this.history[i].id + ' ' + this.history[i].name);
      }
      console.log('---------------')

      let parentCategory = this.history[this.history.length - 1];
      this.currentParentId = parentCategory.parentId;
      this.loadTagList();
      this.history.pop();

      console.log('goBack, done.');
      for (let i = 0; i < this.history.length; i++) {
        console.log(this.history[i].id + ' ' + this.history[i].name);
      }
      console.log('---------------')
    },
    // 切换分页
    changePage(page) {
      this.$router.replace('?page=' + page);
      this.loadTagList();
    },
    // 打开编辑对话框
    openEditDialog(category) {
      let url = 'http://localhost:9090/sys/notice/' + category.id;
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
        } else if (jsonResult.state == 40400) {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.loadTagList();
            }
          });
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    },
    // 打开删除确认框
    openDeleteConfirm(category) {
      let message = '此操作将永久删除【' + category.name + '】类别，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(category);
      }).catch(() => {
      });
    },
    // 处理删除
    handleDelete(category) {
      let url = 'http://localhost:9090/sys/notice/' + category.id + '/delete';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除【' + category.name + '】类别成功！',
            type: 'success'
          });
          this.loadTagList();
        } else if (jsonResult.state == 40400) {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.loadTagList();
            }
          });
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    },
    loadTagList() {
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }
      let url = 'http://localhost:9090/sys/notice?page=' + page;
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data.list;
          this.currentPage = jsonResult.data.currentPage;
          this.pageSize = jsonResult.data.pageSize;
          this.total = jsonResult.data.total;
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    }
  },
  mounted() {
    this.loadTagList();
  }
}
</script>